package zozopo.log.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;

public class OperatorLoginLogOS extends OperatingService{
	
	int securityMaxNum = 240;
	int maxTimes = 10;
	
	
	public OperatorLoginLogOS(Connection conn){
		super(conn);
	}
	
	public OperatorLoginLogOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<OperatorLoginLogTB> selectListBySql(String sql){
		List<OperatorLoginLogTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					list = new ArrayList<OperatorLoginLogTB>();
					while(resultSet.next()){
						OperatorLoginLogTB operatorLoginLog = new OperatorLoginLogTB();
						
						operatorLoginLog.setId(resultSet.getInt(OperatorLoginLogTB.ID));
						operatorLoginLog.setIp(resultSet.getString(OperatorLoginLogTB.IP));
						operatorLoginLog.setAccount(resultSet.getString(OperatorLoginLogTB.ACCOUNT));
						operatorLoginLog.setIswrong(resultSet.getBoolean(OperatorLoginLogTB.ISWRONG));
						operatorLoginLog.setLogindate(resultSet.getString(OperatorLoginLogTB.LOGINDATE));
					
				
						list.add(operatorLoginLog);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return list;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insert(OperatorLoginLogTB operatorLoginLog){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();

		insertMap.put(OperatorLoginLogTB.IP, operatorLoginLog.getIp());
		insertMap.put(OperatorLoginLogTB.ISWRONG, operatorLoginLog.isIswrong());
		insertMap.put(OperatorLoginLogTB.OPERATORID, operatorLoginLog.getOperatorid());
		insertMap.put(OperatorLoginLogTB.ACCOUNT, operatorLoginLog.getAccount());
		insertMap.put(OperatorLoginLogTB.TYPE, operatorLoginLog.getType());
		
		return sqlMachine.insertRow(OperatorLoginLogTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean isWorngOutOfTime(String account){
		boolean isWorngOutOfTime = false;
		
		int worngTime = 0;
		String sql = "SELECT COUNT(account) as worngTime FROM operatorlogin_log WHERE iswrong=TRUE AND account='"+account+"' AND  logindate >= DATE_ADD(NOW(), INTERVAL -1 DAY)" ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					worngTime = resultSet.getInt("worngTime");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(worngTime > maxTimes){
			return true;
		}
		return isWorngOutOfTime;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean isAccountSecurity(String account){
		boolean isAccountSecurity = false;
		
		int worngTime = 0;
		String sql = "SELECT COUNT(account) as worngTime FROM operatorlogin_log WHERE iswrong=TRUE AND account='"+account+"' AND  logindate >= DATE_ADD(NOW(), INTERVAL -1 DAY)" ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					worngTime = resultSet.getInt("worngTime");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(worngTime < securityMaxNum){
			isAccountSecurity = true;
		}
		return isAccountSecurity;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public OperatorLoginLogTB getLastInfo(String account){
		OperatorLoginLogTB operatorLoginLog = new OperatorLoginLogTB();
		

		String sql = "SELECT * FROM "+OperatorLoginLogTB.TABLE_NAME+" WHERE "+OperatorLoginLogTB.ISWRONG+"=FALSE ORDER BY "+OperatorLoginLogTB.LOGINDATE+" DESC LIMIT 0,1" ;
		
		List<OperatorLoginLogTB> list = selectListBySql(sql);
		if(list == null){
			return operatorLoginLog;
		}
		if(list.size() < 1){
			return operatorLoginLog;
		}
		
		operatorLoginLog = list.get(0);
		
		return operatorLoginLog;
	}
}
